package com.web.kongjian.tag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.base.kongjian.entity.Comment;
import com.base.kongjian.entity.Reply;
import com.base.kongjian.entity.Weibo;
import com.base.kongjian.service.CommentService;
import com.base.kongjian.service.ReplyService;
import com.base.kongjian.service.WeiboService;
import com.base.user.entity.Friends;
import com.base.user.entity.GZ;
import com.base.user.entity.Member;
import com.base.user.service.FriendsService;
import com.base.user.service.GZService;
import com.base.user.service.MemberService;
import com.mysteel.core.SpringContext;
import com.mysteel.page.Page;
import com.mysteel.page.PageService;
import com.web.kongjian.ao.utils.KongjianUtils;
import com.web.kongjian.dto.AllFriendsDTO;
import com.web.kongjian.dto.GZDTO;
import com.web.kongjian.dto.WeiboConditionDTO;
import com.web.user.member.utils.CookieUtils;



public class KongjianSeeTag
{
	
	private WeiboService weiboService;
	//private KongjianService kongjianService;
	private MemberService memberService;
	//private MessageService messageService;
	//private WealthSystemService wealthSystemService;
	//private PopularitySystemService popularitySystemService;
	//private WorksNumCountService worksNumCountService;
	//private BlackFriendsService blackFriendsService;
	private FriendsService friendsService;
	private GZService gzService;
	private CommentService commentService;
	private ReplyService replyService;
	private PageService pageService;
	//private WorkShareRecService workShareRecService;
	//private FooterPrintService footerPrintService;
	//private RealInformService realInformService;
	public KongjianSeeTag()
	{
		weiboService = (WeiboService) SpringContext.getBean("weiboService");
		//kongjianService = (KongjianService) SpringContext.getBean("kongjianService");
		memberService = (MemberService) SpringContext.getBean("memberService");
		//messageService = (MessageService) SpringContext.getBean("messageService");
		//wealthSystemService = (WealthSystemService) SpringContext.getBean("wealthSystemService");
		//popularitySystemService = (PopularitySystemService) SpringContext.getBean("popularitySystemService");
		//worksNumCountService = (WorksNumCountService) SpringContext.getBean("worksNumCountService");
		//blackFriendsService = (BlackFriendsService) SpringContext.getBean("blackFriendsService");
		gzService = (GZService) SpringContext.getBean("gzService");
		friendsService = (FriendsService) SpringContext.getBean("friendsService");
		commentService = (CommentService) SpringContext.getBean("commentService");
		replyService = (ReplyService) SpringContext.getBean("replyService");
		pageService = (PageService) SpringContext.getBean("pageService");
		//workShareRecService = (WorkShareRecService) SpringContext.getBean("workShareRecService");
		//footerPrintService = (FooterPrintService) SpringContext.getBean("footerPrintService");
		//realInformService = (RealInformService) SpringContext.getBean("realInformService");
	}
	
	public Map<String, Object> leftTag(HttpServletRequest request)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		
		long memberId = Long.parseLong(request.getParameter ("memberId"));
		
		long id = Long.parseLong (CookieUtils.getCookieValue (request ,"member_id"));
		
		boolean gz = gzService.isEachOtherGZ (id ,memberId);
		
		if(gz==true)
		{
			returnMap.put ("gz" ,"eachOther");
		}else
		{
			gz = gzService.isGZ (id ,memberId);
			if(gz==true)
			{
				returnMap.put ("gz" ,"yes");
			}else
			{
				returnMap.put ("gz" ,"not");
			}
		}
		
		boolean isFriend = friendsService.isFriend (id ,memberId);
		if(isFriend == true)
		{
			returnMap.put ("isFriend" ,"yes");
		}else
		{
			returnMap.put ("isFriend" ,"not");
		}
		
		List<Friends> list = friendsService.getAllFriendByMemberId (memberId);
		long friendsNum = 0L;
		if(ObjectUtils.notEmpty (list)&&ObjectUtils.notNull  (list))
		{
			friendsNum = list.size ();
		}
		returnMap.put ("friendsNum" ,friendsNum);
		
		
		List<GZ> list2 = gzService.getAllGZByMemberId (memberId);
		
		long gzNum = 0L;
		if(ObjectUtils.notEmpty (list2)&&ObjectUtils.notNull (list2))
		{
			gzNum = list2.size ();
		}
		returnMap.put ("gzNum" ,gzNum);
		
		return returnMap;
	}
	
	/**
	 * �����������Ʒ
	 * @param request
	 * @param MaxPageRowCount
	 * @return
	 */
	public Page<WeiboConditionDTO> allWorksTag(HttpServletRequest request,int MaxPageRowCount)
	{
		List<WeiboConditionDTO> listConditionDTOs = new ArrayList<WeiboConditionDTO> ();
		List<Member> listMembers = new ArrayList<Member>();
		long memberId = KongjianUtils.getMemberIdByParams (request);
		Member m = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (m))
		{
			listMembers.add (m);
		}
		
		List<Weibo> weibos = new ArrayList<Weibo>();
		Map<String, Object> params = new HashMap<String,Object> ();
		
		for(Member member : listMembers)
		{
			
			if(ObjectUtils.notNull (member))
			{
				params.put ("memberId" ,member.getId ());
			}
			
			Page<Weibo> p = weiboService.queryWeibo (params ,0 ,-1);
			
			if(ObjectUtils.notNull (p))
			{
				for(Weibo weibo : p.getDatas ())
				{
					weibos.add (weibo); //��������е�΢������
				}
			}
		}
		weibos = weiboSortByPubsherTime (weibos);
		
		long size = weibos.size ();
		long remainder = size%MaxPageRowCount;
		long TotalRowCount = 0L;
		
		if(remainder!=0)
		{
			TotalRowCount = (size/MaxPageRowCount)+1;
		}else
		{
			TotalRowCount = size/MaxPageRowCount;
		}
		String pageString = request.getParameter ("current_page");
		long current_page = 1;
		if(!StringUtils.isTrimEmpty (pageString))
		{
			current_page = Long.parseLong (pageString);
		}
		
		List<Weibo> newWeibos = new ArrayList<Weibo>();
		
		if(current_page==TotalRowCount&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)current_page*MaxPageRowCount-2;i<size;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}else if(current_page==1&&ObjectUtils.notEmpty (weibos))
		{
			for(int i = 0;i<MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		else if(ObjectUtils.notEmpty (weibos))
		{
			for(int i = (int)(current_page*MaxPageRowCount-2);i<(current_page*MaxPageRowCount-2)+MaxPageRowCount;i++)
			{
				newWeibos.add (weibos.get (i));
			}
		}
		if(ObjectUtils.notEmpty (newWeibos))
		{
			for(Weibo w:newWeibos)
			{
				WeiboConditionDTO wct = new WeiboConditionDTO ();
				wct.setMember (memberService.getMemberById (w.getMemberId ()));
				wct.setWeibo (w);
				Map<String, Object> paramsComMap = new HashMap<String,Object>();
				paramsComMap.put ("workId" ,w.getId ());
				paramsComMap.put ("workKind" ,1);
				Page<Comment> pageCommPage = commentService.queryComment (paramsComMap ,0 ,-1);
				List<Comment> comments = new ArrayList<Comment>();
				
				if(ObjectUtils.notNull (pageCommPage))
				{
					comments = pageCommPage.getDatas ();
					wct.setComments (comments);
				}
				
				if(ObjectUtils.notEmpty (comments))
				{
					Map<Long, List<Reply>> replyMap = new HashMap<Long,List<Reply>> ();
					for(Comment c : comments)
					{
						Map<String, Object> paramsreplyMap = new HashMap<String,Object>();
						
						paramsreplyMap.put ("commentId" ,c.getId ());
						
						Page<Reply> pageReply = replyService.queryReply (paramsreplyMap ,0 ,-1);
						
						if(ObjectUtils.notNull (pageReply))
						{
							replyMap.put ((Long)c.getId () ,pageReply.getDatas ());
						}
					}
					
					wct.setReMap (replyMap);
				}
				
				listConditionDTOs.add (wct);
			}
		}
		
		Page<WeiboConditionDTO> pageDTO = new Page<> ();
		
		pageDTO = new Page<WeiboConditionDTO> ();
		pageDTO.setDatas (listConditionDTOs);
		pageDTO.setTotalRowCount ((int)size);
		pageDTO.setCurrentPageNum ((int)current_page);
		pageDTO.setMaxPageRowCount (MaxPageRowCount);
		pageDTO.setURL ("/see/"+memberId+"?&current_page=${pageNum}&conPage=home");
		pageDTO.setTemplate ("pshow");
		pageService.build (pageDTO);
		
		return pageDTO;
	}
	public List<Weibo> weiboSortByPubsherTime(List<Weibo> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getPublishTime ()<list.get (j).getPublishTime ())
				{
					Weibo temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * �û�����
	 * @param request
	 * @return
	 */
	public Map<String, Object> userInforTag(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByParams  (request);
		Map<String, Object> returnMap = new HashMap<String,Object>();
		Member member = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			returnMap.put ("member" ,member);
		}
		
		return returnMap;
	}
	
	/**
	 * �����б�
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<AllFriendsDTO> AllFriendsTag(HttpServletRequest request,int maxPageRowCount)
	{
		Page<AllFriendsDTO> returnPage = null;
		
		long memberId = KongjianUtils.getMemberIdByParams (request);
		
		int currentPageNum = 1;
		String current_pageStr = request.getParameter ("current_page");
		
		if(!StringUtils.isTrimEmpty (current_pageStr))
		{
			currentPageNum = Integer.parseInt (current_pageStr);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		
		Page<Friends> page = friendsService. query (params ,currentPageNum ,maxPageRowCount);
		List<AllFriendsDTO> dtos = null;
		if(ObjectUtils.notNull (page))
		{
			returnPage = new Page<AllFriendsDTO> ();
			dtos = new ArrayList<AllFriendsDTO>();
			List<Friends> list = page.getDatas ();
			list = KongjianTag.friendsSortByCreateTime (list);
			for(Friends fp:list)
			{
				long visitor_MId = fp.getFriend_id  ();
				Member m = memberService.getMemberById (visitor_MId);
				if(ObjectUtils.notNull (m))
				{
					AllFriendsDTO fpdto = new AllFriendsDTO ();
					fpdto.setId (m.getId ());
					String headUrl = m.getHeadUrl ();
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
					}
					
					fpdto.setHeadUrl (headUrl);
					fpdto.setNickName (m.getNickName ());
					fpdto.setVisitorTime (KongjianTag.timeTag (fp.getCreateTime  ()));
					
					dtos.add (fpdto);
				}
			}
			returnPage.setDatas (dtos);
			returnPage.setTotalRowCount (dtos.size ());
			returnPage.setCurrentPageNum (currentPageNum);
			returnPage.setMaxPageRowCount (page.getMaxPageRowCount ());
			returnPage.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=hisFriend");
			returnPage.setTemplate ("pshow");
			pageService.build (returnPage);
		}
		
		return returnPage;
	}
	
	
	/**
	 * �����б�
	 * @param request
	 * @param maxPageRowCount
	 * @return
	 */
	public Page<GZDTO> AllGZTag(HttpServletRequest request,int maxPageRowCount)
	{
		Page<GZDTO> returnPage = null;
		long memberId = KongjianUtils.getMemberIdByParams (request);
		
		int currentPageNum = 1;
		String current_pageStr = request.getParameter ("current_page");
		
		if(!StringUtils.isTrimEmpty (current_pageStr))
		{
			currentPageNum = Integer.parseInt (current_pageStr);
		}
		
		Map<String, Object> params = new HashMap<String,Object>();
		params.put ("memberId" ,memberId);
		
		List<GZ>  allGZS= gzService. getAllGZByMemberId   (memberId);
		
		int size = allGZS.size ();
		int remainder = size%maxPageRowCount;
		long TotalRowCount = 0L;
		
		if(remainder!=0)
		{
			TotalRowCount = (size/maxPageRowCount)+1;
		}else
		{
			TotalRowCount = size/maxPageRowCount;
		}
		
		List<GZ> newgZs = new ArrayList<GZ>();
		
		if(currentPageNum==TotalRowCount&&ObjectUtils.notEmpty (allGZS))
		{
			for(int i = (int)currentPageNum*maxPageRowCount-maxPageRowCount;i<size;i++)
			{
				newgZs.add (allGZS.get (i));
			}
		}else if(currentPageNum==1&&ObjectUtils.notEmpty (allGZS))
		{
			for(int i = 0;i<maxPageRowCount;i++)
			{
				newgZs.add (allGZS.get (i));
			}
		}
		else if(ObjectUtils.notEmpty (allGZS))
		{
			for(int i = (int)(currentPageNum*maxPageRowCount-maxPageRowCount);i<(currentPageNum*maxPageRowCount-2)+maxPageRowCount;i++)
			{
				newgZs.add (allGZS.get (i));
			}
		}
		
		List<GZDTO> dtos = null;
		if(ObjectUtils.notEmpty  (newgZs))
		{
			returnPage = new Page<GZDTO> ();
			dtos = new ArrayList<GZDTO>();
			List<GZ> list = newgZs;
			list = GZSortByCreateTime (list);
			for(GZ fp:list)
			{
				long visitor_MId = fp.getFriend_id  ();
				Member m = memberService.getMemberById (visitor_MId);
				if(ObjectUtils.notNull (m))
				{
					GZDTO fpdto = new GZDTO ();
					fpdto.setId (m.getId ());
					String headUrl = m.getHeadUrl ();
					if(StringUtils.isTrimEmpty (headUrl))
					{
						headUrl = "/skin/images/kongjian/noavatar_small.gif";
					}
					
					fpdto.setHeadUrl (headUrl);
					fpdto.setNickName (m.getNickName ());
					fpdto.setVisitorTime (KongjianTag.timeTag (fp.getCreateTime  ()));
					
					dtos.add (fpdto);
				}
			}
			returnPage.setDatas (dtos);
			returnPage.setTotalRowCount (dtos.size ());
			returnPage.setCurrentPageNum (currentPageNum);
			returnPage.setMaxPageRowCount (maxPageRowCount);
			returnPage.setURL ("/u/"+memberId+"?&current_page=${pageNum}&conPage=gz");
			returnPage.setTemplate ("pshow");
			pageService.build (returnPage);
		}
		
		return returnPage;
	}
	
	public List<GZ> GZSortByCreateTime(List<GZ> list)
	{
		for(int i = 0;i<list.size ();i++)
		{
			for(int j = 1;j<list.size ();j++)
			{
				if(list.get (j-1).getCreateTime    ()<list.get (j).getCreateTime  ())
				{
					GZ temp = null;
					temp = list.get (j-1);
					list.set (j-1 ,list.get (j));
					list.set (j ,temp);
				}
			}
		}
		
		return list;
	}
}
